package com.mxx.qh.network.http;

import com.mxx.qh.App;
import com.mxx.qh.SPUtils;
import com.mxx.qh.config.ConstantsConfig;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @ClassName: AddCookiesInterceptor
 * @Description: java类作用描述
 * @Author: ljf89 (刘江飞)
 * @Date: 2021/1/13-- 9:26
 * @Email: 642371742@qq.com
 */
public class AddCookiesInterceptor implements Interceptor {

    @NotNull
    @Override
    public Response intercept(Chain chain) throws IOException {

        Request.Builder builder = chain.request().newBuilder();
        String cookie = (String) SPUtils.get(App.getContext(), ConstantsConfig.COOKIE_TOKEN, "");
        String x_crsf_token = (String) SPUtils.get(App.getContext(), ConstantsConfig.X_CSRF_TOKEN, "");
        if (cookie != null && !cookie.isEmpty()
                && x_crsf_token != null && !x_crsf_token.isEmpty()) {
            builder.addHeader("Cookie", cookie + ";" + x_crsf_token);
            String token=x_crsf_token.replace("csrftoken=","");
            builder.addHeader("X-CSRFToken", token.trim());
        }
        return chain.proceed(builder.build());
    }
}
